package com.finace.service;

import com.finace.management.service.UserService;
import com.finace.pojo.TbUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.core.userdetails.User;
import java.util.ArrayList;
import java.util.List;
/**
 * 认证类
 * @author Administrator
 *
 */
public class UserDetailServicelmpl implements UserDetailsService {


        private UserService userService;

        public void setUserService(UserService userService) {
            this.userService = userService;
        }

        @Override
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            System.out.println("经过了UserDetailsServiceImpl");
            //构建角色列表

            //得到商家对象
           TbUser tbUser=userService.findByuserName(username);
            if (tbUser != null) {

                    List<GrantedAuthority> grantAuths = new ArrayList();

                    grantAuths.add(new SimpleGrantedAuthority("ROLE_SELLER"));

                    return new User(username, tbUser.getPassword(), grantAuths);
            } else {
                return null;
            }
        }


    }
